﻿package com.hatch.ui
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
     * HighlightTextField
     * 
     * generates a Sprite containing a textField with a padded background
     * I should refactor this to a uility that applies a background
     * to a provided textfield
     *	
     * @author Andy Hatch
     */

	public class HighlightText extends Sprite
	{
		private var _format:TextFormat;
		private var _text:String;
		private var _hiColor:uint;
		private var _indent:Number;
		private var _vertSpace:Number;
		
		private var _horizPadding:Number = 8;
		private var _vertPadding:Number = 5;
		
		private var _highlightAlpha:Number = .8;
		
		private var _strings:Array; //array of strings split by <br>
		private var _backs:Array;	//array of highlight sprites
		private var _lines:Array;	//array holding the sprites = textfields with sprite backgrounds
		
		public function HighlightText(text:String = "", hiColor:uint = 0x12345, format:TextFormat = null, vertSpace:Number = 3, indent:Number = 0 )
		{
			_strings = new Array();
			_lines = new Array();
			_backs = new Array();
			
			_text = text;
			_hiColor = hiColor;
			if(format){ _format = format; }
			else{ _format = defaultFormat(); }
			_indent = indent;
			_vertSpace = vertSpace;
			
			if(_text == ""){ return; } else { buildHighlight(); }
		}
		
		private function buildHighlight():void
		{
			var charCounter:uint = 0;
			var field:TextField;
			var line:Sprite;
			
			//parse string
			if( _text.indexOf("<br", 0) == 1 ){	_strings.push(_text); }
			else{ _strings = _text.split("<br/>"); }
			
			
			for each(var s:String in _strings)
			{
				field = new TextField();
				field.autoSize = TextFieldAutoSize.LEFT;
				field.multiline = true;
				field.selectable = false;
				//_tf.embedFonts = true;
				//add text to field
				field.htmlText = s;
				
				line = getLine(field)
 				_lines.push(line);
 				this.addChild(line);
 			} 
 			
 			space();
			animateIn();
		}
		
		private function space():void
		{
			var vertRuler:Number = 0;
			var line:Sprite;
			
			for(var i:uint = 0; i < _lines.length; i++)
			{
				line = _lines[i];
				
				//indent on first line
				if(i == 0 && _indent > 0){ line.x = _indent }
				line.y = vertRuler;
				vertRuler += line.height + _vertSpace;
			}
		}
		
		private function animateIn():void
		{
			for each(var l:Sprite in _lines)
			{
				
			}
		}
		
		private function animateOut():void
		{
			
		}
		
		private function getLine(field:TextField):Sprite
		{
			//var field:TextField = f;
			var line:Sprite = new Sprite();
			var back:Sprite = new Sprite();
			
			//build back
			back.graphics.beginFill(_hiColor);
			back.graphics.drawRect(0,0,field.width + _horizPadding*2, field.height + _vertPadding*2);
			back.graphics.endFill();
			back.alpha = _highlightAlpha;
			
			_backs.push(back);
			
			field.x = _horizPadding;
			field.y = _vertPadding;
			
			line.addChild(back);
			line.addChild(field);
			
			return line;
		}
		
		private function defaultFormat():TextFormat
		{
			var format:TextFormat = new TextFormat();
			format.color = 0xffffff;
			format.size = 20;
			format.align = TextFormatAlign.LEFT;
			return format;
		}
		
		public function set highlightAlpha(a:Number):void
		{
			_highlightAlpha = a;
			
			for each(var b:Sprite in _backs)
			{
				b.alpha = _highlightAlpha;
			}
			
		}
		
		public function set vertSpace(n:Number):void
		{
			_vertSpace = n;
			space();
		}
		
		public function set indent(n:Number):void
		{
			_indent = n;
			space();
		}
		
		
	}

	
}